package com.sne.airways.basecore.service.impl;

import com.sne.airways.basecore.dao.BaseDAO;
import com.sne.airways.basecore.exceptions.AppException;
import com.sne.airways.basecore.pojo.BaseProperties;
import com.sne.airways.basecore.service.BaseService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: 卞京帅 {bjslogin#gmail.com}
 * Date: 12-11-11
 * Time: 下午8:49
 * To change this template use File | Settings | File Templates.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BaseServiceImpl implements BaseService {
    protected final Logger logger = Logger.getLogger(BaseServiceImpl.class);
    @Autowired
    @Qualifier(value = "baseDAOHibernate")
    private BaseDAO baseDAO;

    public <T> T loadObject(Class<T> clazz, Serializable id) {
        return baseDAO.loadObject(clazz,id);
    }

    public <T> T getObject(Class<T> clazz, Serializable id) {
        return baseDAO.getObject(clazz,id);
    }

    public void saveObject(Object o) {
        baseDAO.saveObject(o);
    }

    public void saveCollection(Collection collection) {
        baseDAO.saveCollection(collection);
    }

    public void updateObject(Object o) {
        baseDAO.updateObject(o);
    }

    public void deleteObject(Object o) {
        baseDAO.deleteObject(o);
    }

    public void deleteCollection(Collection collection) {
        baseDAO.deleteCollection(collection);
    }

    public <T> List<T> findByExample(T o) {
        return baseDAO.findByExample(o);
    }

    public <T> List<T> findByHQL(Class<T> clazz, Object... param) {
        return baseDAO.findByHQL(clazz,param);
    }

    public <T> List<T> findByHQL(Class<T> clazz, Integer start, Integer limit, Object... param) {
        return baseDAO.findByHQL(clazz,start,limit,param);
    }

    public <T> List<T> findByHQL(Class<T> clazz, Map orderBy, Object... param) {
        return baseDAO.findByHQL(clazz,orderBy,param);
    }

    public <T> List<T> findByHQL(Class<T> clazz, Integer start, Integer limit, Map orderBy, Object... param) {
        return baseDAO.findByHQL(clazz,start,limit,orderBy,param);
    }

    @Override
    public void isCurrentVersion(BaseProperties object, int version) throws AppException{
        if(object.getVersion()!=version) throw new AppException("当前版本的对象已经不存在,请刷新列表后重新操作!");
    }

    public Integer findCountByHQL(Class clazz, Object... param){
        return baseDAO.findCountByHQL(clazz,param);
    }

    /**
     * 组装map 对应 key,value,key,value,key,value...类型
     * @param param 不定量参数
     * @return map
     */
    protected HashMap<String,Object> assemblyMap(Object... param){
        HashMap<String,Object> map = new HashMap<String,Object>();
        for(int i = 0 ; i < param.length ; i += 2){
            map.put(param[i].toString(),param[ i + 1 ]);
        }
        return map;
    }
}
